package gui;

import bean.Book;
import bean.Borrowed;
import bean.User;

import java.awt.*;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import service.BookService;
import service.BorrowedService;
import service.UserService;
import util.DateUtil;
import util.FrmUtil;
import util.StringUtil;

/**
 * 图书借阅主界面.
 * 
 * @author 
 */
public class BorrMgrPnl extends javax.swing.JPanel {
    private User user_login;
    private UserService userService;
    private BookService bookService;
    private BorrowedService borrService;
    
    public void init(){
        if(!user_login.getType().equals(FrmUtil.OPRATOR_USER)){
             btnBorrBook.setEnabled(false);
             btnBorrAgain.setEnabled(false);
             btnBorrReturn.setEnabled(false);
             
             txtBorrDate.setEditable(false);
             txtReturnDate.setEditable(false);
         }
        txtBorrDate.setText(DateUtil.getCurrentDate());
        txtReturnDate.setText(DateUtil.getLeaveMonth(DateUtil.getCurrentDate(), 1));
    }
    /**
     * Creates new form BorrMgrPnl
     */
    public BorrMgrPnl(User user) {
        user_login = user;
        initComponents();
        init();
        userService = new UserService();
        bookService = new BookService();
        borrService = new BorrowedService();
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtUserId = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtBookId = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtBorrDate = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtReturnDate = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtUserName = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtBookName = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        txtAuthor = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        txtPublisher = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        txtPrice = new javax.swing.JTextField();
        btnBorrBook = new javax.swing.JButton();
        btnBorrAgain = new javax.swing.JButton();
        btnBorrQry = new javax.swing.JButton();
        btnBorrReturn = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblBorrs = new javax.swing.JTable();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("图书借还"));

        jLabel1.setText("读者编号");

        txtUserId.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtUserIdActionPerformed(evt);
            }
        });
        txtUserId.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtUserIdFocusLost(evt);
            }
        });

        jLabel3.setText("图书编号");

        txtBookId.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtBookIdActionPerformed(evt);
            }
        });
        txtBookId.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtBookIdFocusLost(evt);
            }
        });

        jLabel4.setText("借书日期");

        jLabel5.setText("还书日期");

        jLabel6.setText("读者姓名");

        txtUserName.setEditable(false);

        jLabel7.setText("书名");

        txtBookName.setEditable(false);

        jLabel8.setText("作者");

        txtAuthor.setEditable(false);

        jLabel9.setText("出版社");

        txtPublisher.setEditable(false);

        jLabel10.setText("单价");

        txtPrice.setEditable(false);

        btnBorrBook.setText("图书借阅");
        btnBorrBook.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBorrBookActionPerformed(evt);
            }
        });

        btnBorrAgain.setText("图书续借");
        btnBorrAgain.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBorrAgainActionPerformed(evt);
            }
        });

        btnBorrQry.setText("借阅查询");
        btnBorrQry.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBorrQryActionPerformed(evt);
            }
        });

        btnBorrReturn.setText("图书归还");
        btnBorrReturn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBorrReturnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(101, 101, 101)
                        .addComponent(btnBorrBook)
                        .addGap(79, 79, 79)
                        .addComponent(btnBorrAgain)
                        .addGap(110, 110, 110)
                        .addComponent(btnBorrReturn)
                        .addGap(73, 73, 73)
                        .addComponent(btnBorrQry))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(txtReturnDate, javax.swing.GroupLayout.PREFERRED_SIZE, 229, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(jLabel4)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(txtBorrDate, javax.swing.GroupLayout.PREFERRED_SIZE, 229, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel3)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(txtBookId, javax.swing.GroupLayout.PREFERRED_SIZE, 229, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel1)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(txtUserId, javax.swing.GroupLayout.PREFERRED_SIZE, 229, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(13, 13, 13)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel7)
                                    .addComponent(jLabel9))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(txtPublisher, javax.swing.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE)
                                    .addComponent(txtBookName))
                                .addGap(24, 24, 24)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel8)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(txtAuthor, javax.swing.GroupLayout.PREFERRED_SIZE, 229, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel10)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(txtPrice, javax.swing.GroupLayout.PREFERRED_SIZE, 229, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtUserName, javax.swing.GroupLayout.PREFERRED_SIZE, 229, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(30, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtUserId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(txtUserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(txtBookName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtAuthor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3)
                        .addComponent(txtBookId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(txtBorrDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(txtReturnDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel10, javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtPrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtPublisher, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jLabel9))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnBorrAgain)
                        .addComponent(btnBorrReturn)
                        .addComponent(btnBorrQry))
                    .addComponent(btnBorrBook))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("借阅信息查询结果"));

        tblBorrs.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "借阅人", "ISBN", "书名", "作者", "出版社", "借书日期", "还书日期"
            }
        ));
        jScrollPane1.setViewportView(tblBorrs);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 477, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(36, 36, 36))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnBorrBookActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBorrBookActionPerformed
        // 图书借阅
        String strUserId = txtUserId.getText().trim();
        if(!StringUtil.hasLength(strUserId)){
            JOptionPane.showMessageDialog(this, "用户编号不能为空！");
            txtUserId.requestFocus();
            return;
        }
        User user = userService.queryUserById(strUserId);
        if(user == null){
            JOptionPane.showMessageDialog(this, "用户不存在！");
            txtUserId.requestFocus();
            return;
        }
        String strBookId = txtBookId.getText().trim();
        if(!StringUtil.hasLength(strBookId)){
            JOptionPane.showMessageDialog(this, "图书编号不能为空！");
            txtBookId.requestFocus();
            return;
        }
        Book book = bookService.queryBookById(strBookId);
        if(book == null){
            JOptionPane.showMessageDialog(this, "图书不存在！");
            txtBookId.requestFocus();
            return;
        }
        if(borrService.queryBorrowedsOfBook(strBookId).size() > 0 ){//已经被借阅
            JOptionPane.showMessageDialog(this, "该书已被借阅！");
            txtBookId.requestFocus();
            return;
        }
        
        String strBorrDate = txtBorrDate.getText().trim();
        if(!StringUtil.hasLength(strBorrDate) || !DateUtil.isValidDate(strBorrDate)){
            JOptionPane.showMessageDialog(this, "借书日期不能为空或格式错误！");
            txtBorrDate.requestFocus();
            return;
        }
        //日期检查
        if(!strBorrDate.equals(DateUtil.getCurrentDate())){
            JOptionPane.showMessageDialog(this, "借书日期必须是今天！");
            txtBorrDate.requestFocus();
            return;
        }
        String strReturnDate = txtReturnDate.getText().trim();
        if(!StringUtil.hasLength(strReturnDate) || !DateUtil.isValidDate(strReturnDate)){
            JOptionPane.showMessageDialog(this, "还书日期不能为空或格式错误！");
            txtReturnDate.requestFocus();
            return;
        }
        if(strReturnDate.compareTo(strBorrDate) <=0 ){
            JOptionPane.showMessageDialog(this, "还书日期必须大于借书日期！");
            txtReturnDate.requestFocus();
            return;
        }
        
        if(borrService.BorrowBook(new Borrowed(strBookId,strUserId,strBorrDate,strReturnDate))){
            JOptionPane.showMessageDialog(this, "借阅成功！");
        }else{
            JOptionPane.showMessageDialog(this, "借阅失败！");
        }
        
    }//GEN-LAST:event_btnBorrBookActionPerformed

    private void txtUserIdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtUserIdActionPerformed
        // TODO add your handling code here:
        opUserAfterEnterOrLost();
    }//GEN-LAST:event_txtUserIdActionPerformed

    private void txtUserIdFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtUserIdFocusLost
        // TODO add your handling code here:
        opUserAfterEnterOrLost();
    }//GEN-LAST:event_txtUserIdFocusLost

    private void txtBookIdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtBookIdActionPerformed
        // TODO add your handling code here:
        opBookAfterEnterOrLost();
    }//GEN-LAST:event_txtBookIdActionPerformed

    private void txtBookIdFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtBookIdFocusLost
        // TODO add your handling code here:
        opBookAfterEnterOrLost();
    }//GEN-LAST:event_txtBookIdFocusLost

    private void btnBorrAgainActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBorrAgainActionPerformed
        // 续借
        String strUserId = txtUserId.getText().trim();
        if(!StringUtil.hasLength(strUserId)){
            JOptionPane.showMessageDialog(this, "用户编号不能为空！");
            txtUserId.requestFocus();
            return;
        }
        User user = userService.queryUserById(strUserId);
        if(user == null){
            JOptionPane.showMessageDialog(this, "用户不存在！");
            txtUserId.requestFocus();
            return;
        }
        String strBookId = txtBookId.getText().trim();
        if(!StringUtil.hasLength(strBookId)){
            JOptionPane.showMessageDialog(this, "图书编号不能为空！");
            txtBookId.requestFocus();
            return;
        }
        Book book = bookService.queryBookById(strBookId);
        if(book == null){
            JOptionPane.showMessageDialog(this, "图书不存在！");
            txtBookId.requestFocus();
            return;
        }
        if(borrService.queryOneBorrowed(strUserId, strBookId).size() == 0 ){//读者没有借阅该书
            JOptionPane.showMessageDialog(this, "该读者还没有借阅该书，不能续借！");
            txtBookId.requestFocus();
            return;
        }
        
        String strBorrDate = txtBorrDate.getText().trim();
        if(!StringUtil.hasLength(strBorrDate) || !DateUtil.isValidDate(strBorrDate)){
            JOptionPane.showMessageDialog(this, "借书日期不能为空或格式错误！");
            txtBorrDate.requestFocus();
            return;
        }
        //日期检查
        if(!strBorrDate.equals(DateUtil.getCurrentDate())){
            JOptionPane.showMessageDialog(this, "借书日期必须是今天！");
            txtBorrDate.requestFocus();
            return;
        }
        String strReturnDate = txtReturnDate.getText().trim();
        if(!StringUtil.hasLength(strReturnDate) || !DateUtil.isValidDate(strReturnDate)){
            JOptionPane.showMessageDialog(this, "还书日期不能为空或格式错误！");
            txtReturnDate.requestFocus();
            return;
        }
        if(strReturnDate.compareTo(strBorrDate) <= 0 ){
            JOptionPane.showMessageDialog(this, "还书日期必须大于借书日期！");
            txtReturnDate.requestFocus();
            return;
        }
        
        if(borrService.BorrowAgain(new Borrowed(strBookId,strUserId,strBorrDate,strReturnDate))){
            JOptionPane.showMessageDialog(this, "续借成功！");
        }else{
            JOptionPane.showMessageDialog(this, "续借失败！");
        }
    }//GEN-LAST:event_btnBorrAgainActionPerformed

    private void btnBorrReturnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBorrReturnActionPerformed
        // 还书
        String strUserId = txtUserId.getText().trim();
        if(!StringUtil.hasLength(strUserId)){
            JOptionPane.showMessageDialog(this, "用户编号不能为空！");
            txtUserId.requestFocus();
            return;
        }
        User user = userService.queryUserById(strUserId);
        if(user == null){
            JOptionPane.showMessageDialog(this, "用户不存在！");
            txtUserId.requestFocus();
            return;
        }
        String strBookId = txtBookId.getText().trim();
        if(!StringUtil.hasLength(strBookId)){
            JOptionPane.showMessageDialog(this, "图书编号不能为空！");
            txtBookId.requestFocus();
            return;
        }
        Book book = bookService.queryBookById(strBookId);
        if(book == null){
            JOptionPane.showMessageDialog(this, "图书不存在！");
            txtBookId.requestFocus();
            return;
        }
        List<Borrowed> borr = borrService.queryOneBorrowed(strUserId, strBookId);
        
        if( borr.size() == 0 ){//读者没有借阅该书
            JOptionPane.showMessageDialog(this, "该读者还没有借阅该书，不能还书！");
            txtBookId.requestFocus();
            return;
        }
        
        String strBorrDate = txtBorrDate.getText().trim();
        if(!StringUtil.hasLength(strBorrDate) || !DateUtil.isValidDate(strBorrDate)){
            JOptionPane.showMessageDialog(this, "借书日期不能为空或格式错误！");
            txtBorrDate.requestFocus();
            return;
        }
        //还书时检查借书日期是否一致
        if(!strBorrDate.equals(borr.get(0).getBorrow_date())){
            JOptionPane.showMessageDialog(this, "借书日期不正确！");
            txtBorrDate.requestFocus();
            return;
        }
        String strReturnDate = txtReturnDate.getText().trim();
        if(!StringUtil.hasLength(strReturnDate) || !DateUtil.isValidDate(strReturnDate)){
            JOptionPane.showMessageDialog(this, "还书日期不能为空或格式错误！");
            txtReturnDate.requestFocus();
            return;
        }
        if(!strReturnDate.equals(DateUtil.getCurrentDate()) ){
            JOptionPane.showMessageDialog(this, "还书日期必须是今天！");
            txtReturnDate.requestFocus();
            return;
        }
        
        if(borrService.BorrowReturn(new Borrowed(strBookId,strUserId,strBorrDate,strReturnDate))){
            JOptionPane.showMessageDialog(this, "还书成功！");
        }else{
            JOptionPane.showMessageDialog(this, "还书失败！");
        }
    }//GEN-LAST:event_btnBorrReturnActionPerformed

    private void btnBorrQryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBorrQryActionPerformed
        // 查询
        String strUserId = txtUserId.getText().trim();
        
        String strBookId = txtBookId.getText().trim();
        
        List<Borrowed> borrs = null;
        if(!StringUtil.hasLength(strUserId) && !StringUtil.hasLength(strBookId)){
           borrs = borrService.queryAllBorroweds();
        }else if(StringUtil.hasLength(strUserId) && !StringUtil.hasLength(strBookId)){
            borrs = borrService.queryBorrowedsOfUser(strUserId);
        }else if(!StringUtil.hasLength(strUserId) && StringUtil.hasLength(strBookId)){
            borrs = borrService.queryBorrowedsOfBook(strBookId);
        }else{
            borrs = borrService.queryOneBorrowed(strUserId, strBookId);
        }
        showBorroweds(borrs);
        
    }//GEN-LAST:event_btnBorrQryActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBorrAgain;
    private javax.swing.JButton btnBorrBook;
    private javax.swing.JButton btnBorrQry;
    private javax.swing.JButton btnBorrReturn;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblBorrs;
    private javax.swing.JTextField txtAuthor;
    private javax.swing.JTextField txtBookId;
    private javax.swing.JTextField txtBookName;
    private javax.swing.JTextField txtBorrDate;
    private javax.swing.JTextField txtPrice;
    private javax.swing.JTextField txtPublisher;
    private javax.swing.JTextField txtReturnDate;
    private javax.swing.JTextField txtUserId;
    private javax.swing.JTextField txtUserName;
    // End of variables declaration//GEN-END:variables

    /**
     * 用户编号文本框回车或丢失焦点后的执行的操作.
     *
     * 查询用户是否存在，如果存在显示用户相关信息<br>
     */
    private void opUserAfterEnterOrLost() {
        String strUserId = txtUserId.getText().trim();
        if(!StringUtil.hasLength(strUserId)){
            return;
        }
        User user = userService.queryUserById(strUserId);
        if(user == null){
            return;
        }
        txtUserName.setText(user.getName());
    }
    /**
     * 图书编号文本框回车或丢失焦点后的执行的操作.
     *
     * 查询图书是否存在，如果存在显示图书相关信息<br>
     */
    private void opBookAfterEnterOrLost() {
        String strBookId = txtBookId.getText().trim();
        if(!StringUtil.hasLength(strBookId)){
            return;
        }
        Book book = bookService.queryBookById(strBookId);
        if(book == null){
            return;
        }
        txtBookName.setText(book.getName());
        txtAuthor.setText(book.getAuthor());
        txtPublisher.setText(book.getPublisher());
        txtPrice.setText(book.getPrice()+"");
        
    }
    
    /**
     * 表格显示借阅信息.
     * 
     * @param borrs 
     */
    private void showBorroweds(List<Borrowed> borrs) {
        String colums[] = {"借阅人","ISBN","书名","作者","出版社","借书日期","还书日期"};
        
        String rowData[][] = new String[borrs.size()][colums.length];
        
        for(int i = 0; i < rowData.length; i++){
            Borrowed borr = borrs.get(i);
            rowData[i][0] = borr.getUser().getName();
            rowData[i][1] = borr.getBook().getId();
            rowData[i][2] = borr.getBook().getName();
            rowData[i][3] = borr.getBook().getAuthor();
            rowData[i][4] = borr.getBook().getPublisher();
            rowData[i][5] = borr.getBorrow_date();
            rowData[i][6] = borr.getReturn_date();
        }
        DefaultTableModel model = new DefaultTableModel(rowData, colums){
            @Override//重写方法，使得表格不能被编辑
            public boolean isCellEditable(int row, int column)
            {
                return false;
            }
        };
        tblBorrs.setRowHeight(30);
        Font font = new Font("宋体", Font.PLAIN, 16);
        tblBorrs.setFont(font);
        tblBorrs.setModel(model);
    }
}
